import uuid

from django.shortcuts import render

# Create your views here.

from rest_framework.views import APIView
from rest_framework.response import Response
from authDemo import models
from .auth import MyAuthentication
from .permission import MyPermission


class LoginView(APIView):
    authentication_classes = []
    def post(self, request, *args, **kwargs):
        # 以字典类型获取传参
        data = request.POST.dict()
        user_obj = models.UserInfo.objects.filter(**request.data).first()
        if not user_obj:
            return Response('登录失败')
        random_str = str(uuid.uuid4())
        user_obj.token = random_str
        user_obj.save()
        return Response(random_str)


class OrdersView(APIView):
    # authentication_classes = [MyAuthentication, ]
    def get(self, request, *args, **kwargs):
        # 如果认证试图没有调用request.user，那么认证流程就不会走
        if request.user:
            print(request.user)
            print(request.auth)
            return Response('user')

        return Response('没有token')


class UserView(APIView):
    # 如果某个接口不需要认证，只需要添加这句代码就好
    authentication_classes = []
    pass


from rest_framework.throttling import AnonRateThrottle


class FangWenView(APIView):
    # 网站的访问限制
    # throttle_classes = [AnonRateThrottle, ]

    def get(self, request, *args, **kwargs):
        return Response('访问限制')
